<script setup>
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import {computed, ref} from 'vue'

//用户搜索
const username=ref('')

//列表数据模型
const users = ref([
  {
    "id": '',
    "username": "",
    "password": "",
    "nickname": "",
    "sex": "",
    "locate": " ",
    "phone": "",
    "email": "",
    "hobby":"",
    "user_pic": "",
    "createTime": "",
    "updateTime": ""
  }
])

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(5)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  userList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  userList()
}

//获取文章列表
import {userListService} from '@/api/user.js'
import { formatDate } from'@/utils/formdata'
//导入用户状态
const userList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    username: username.value ? username.value : null
  }
  let result = await userListService(params);

  //渲染视图
  total.value = result.data.total;
  users.value = result.data.items;
  users.value.forEach((item) => {
    item.createTime = formatDate(item.createTime)
  });
  console.log(result);
}
//调用函数
userList()//获取列表
//删除用户
import {ElMessage, ElMessageBox} from 'element-plus'
import {articleDeleteService} from "@/api/article.js";
const deleteUser = async (row) => {
  //提示用户 确认框
  ElMessageBox.confirm(
      '你确认要删除该用户吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let result = await articleDeleteService(row.id);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        await userList();
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了删除',
        })
      })
}


import {Plus} from '@element-plus/icons-vue'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref()
//定义抽屉标题显示
const title = ref('')

//展示抽屉
const showDialog = (row) => {
  visibleDrawer.value = true;
  title.value = '编辑用户';
  userModel.value.id = row.id;
  userModel.value.username = row.username;
  userModel.value.password = row.password;
  userModel.value.nickname = row.nickname;
  userModel.value.sex = row.sex;
  userModel.value.locate = row.locate;
  userModel.value.phone = row.phone;
  userModel.value.hobby = row.hobby;
  userModel.value.email = row.email;
  userModel.value.userPic = row.userPic;

}

const rules = {
  nickname: [
    { required: true, message: '请输入用户昵称', trigger: 'blur' },
    {
      pattern: /^\S{2,10}$/,
      message: '昵称必须是2-10位的非空字符串',
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入用户邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ]
}

//添加表单数据模型
const userModel = ref({
  id: '',
  username: '',
  password: '',
  nickname: '',
  sex: '',
  locate: '',
  phone: '',
  hobby:'',
  email:'',
  userPic:''
})

//上传图片成功地回调函数
const uploadSuccess = (result) => {
  userModel.value.userPic = result.data;
  console.log(result.data);
}

//修改个人信息
import {userInfoUpdateService} from '@/api/user.js'
const updateUserInfo = async ()=> {
  console.log(userModel.value);
  //调用接口
  let result = await userInfoUpdateService(userModel.value);
  ElMessage.success(result.msg ? result.msg : '修改成功');
  visibleDrawer.value = false;
  await userList()
}

//导入token
  import {useTokenStore} from "@/stores/token.js";
  const tokenStore = useTokenStore()

//清空模型的数据
  const quillEditorKey = ref(0);
  const clearData = () => {
    userModel.value.id = '';
    userModel.value.username = '';
    userModel.value.password = '';
    userModel.value.nickname = '';
    userModel.value.sex = '';
    userModel.value.locate = '';
    userModel.value.phone = '';
    userModel.value.hobby = '';
    userModel.value.email = '';
    quillEditorKey.value++;
  }
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>用户管理</span>
        <!-- 搜索表单 -->
        <el-form inline>
          <el-form-item label="用户名">
            <el-input placeholder="请输入" v-model="username" style="width: 200px">
            </el-input>
          </el-form-item>
          <el-form-item style="margin: 0px;">
            <el-button type="primary" @click="userList()">搜索</el-button>
            <el-button @click="userList();clearData()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table :data="users" height="350px" style="width: 100%">
      <el-table-column sortable label="序号"  prop="id"></el-table-column>
      <el-table-column sortable label="注册时间" prop="createTime"></el-table-column>
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column sortable label="性别" prop="sex"> </el-table-column>
      <el-table-column label="爱好" prop="hobby"></el-table-column>
      <el-table-column label="昵称" prop="nickname"></el-table-column>
      <el-table-column label="头像" align="center">
        <template #default="{ row }">
          <img style="width: 40px;height: 40px;border-radius: 50%;object-fit: cover;" :src="row.userPic" class="avatar" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" >
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
<!--          <el-button :icon="Delete" circle plain type="danger"></el-button>-->
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="30%">
      <!-- 添加文章表单 -->
      <el-form label-width="100px" :model="userModel" :rules="rules">
        <el-form-item label="id" >
          <el-input disabled style="width: 200px" placeholder="请输入" v-model="userModel.id"></el-input>
        </el-form-item>
        <el-form-item label="用户" >
          <el-input disabled style="width: 200px" placeholder="请输入" v-model="userModel.username"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input style="width: 200px" placeholder="请输入" v-model="userModel.nickname"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="userModel.sex">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="爱好">
          <el-input style="width: 200px" v-model="userModel.hobby" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="位置">
          <el-input style="width: 200px" placeholder="请输入" v-model="userModel.locate"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input style="width: 200px" placeholder="请输入" v-model="userModel.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input style="width: 200px" placeholder="请输入" v-model="userModel.email"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <!--
              auto-upload:设置是否自动上传
              action:设置服务器接口路径
              name:设置上传的文件字段名
              headers:设置上传的请求头
              on-success:设置上传成功的回调函数
           -->
          <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false" action="/api/image/upload" name="file" :headers="{'Authorization': tokenStore.token}" :on-success="uploadSuccess">
            <img v-if="userModel.userPic" :src="userModel.userPic" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">
              <Plus />
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateUserInfo">更改</el-button>
          <el-button type="info" @click="visibleDrawer=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
::v-deep .el-form-item{
    margin: 5px 5px ;
    font-size: 13px;
}
</style>